﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Commerce.Data;

namespace Commerce.CSK.Web
{
    public partial class ViewProduct : ItemViewPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int productId = -1; 
                if(int.TryParse(Request.QueryString["p"], out productId)){
                    _selectedProduct = Services.GetCatalogService().GetProduct(productId);
                    Services.ProductViewed(productId);
                    this.DataBind(); 
                }
            }
            
        }

        private Product _selectedProduct;
        public Product SelectedProduct
        {
            get
            {
                if (_selectedProduct == null)
                {
                    int? productId = ItemId;
                    if (productId != null && productId.HasValue)
                    {
                        _selectedProduct = Services.GetCatalogService().GetProduct(productId.Value); 
                    }
                }
                return _selectedProduct;
            }
            set
            {
                _selectedProduct = value;
                if (value != null)
                {
                    ItemId = value.ID;
                    Services.ProductViewed(value.ID);
                }
            }
        }
    }
}
